import time
import thread
import threading

def timer1(n, ID):
    cnt = 0
    while cnt<n:
        print 'Thread:(%d) Time:%s\n'%(ID,time.ctime())
        cnt+=1
    thread.exit_thread()
   
class timer2(threading.Thread): #The timer class is derived from the class threading.Thread
    def __init__(self, ID):
        threading.Thread.__init__(self)
        self.m_ID = ID
        self.m_stop = False

    def run(self):
        while not self.m_stop:
            time.sleep(2)
            print 'Thread Object(%d), Time:%s\n' %(self.m_ID, time.ctime())

    def stop(self):
        self.m_stop = True
    
def test(): #Use thread.start_new_thread() to create 2 new threads
    #thread.start_new_thread(timer1, (5,1))
    #thread.start_new_thread(timer1, (5,2))
    thread1 = timer2(1)
    thread2 = timer2(2)
    thread1.start()
    thread2.start()
    time.sleep(5)
    thread1.stop()
    thread2.stop()
 
if __name__=='__main__':
    test()
 